package com.koron.order.risk.base.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.util.CodeTools;
import com.koron.order.common.tools.CommonCodeTools;
import com.koron.order.common.tools.OrderCodeEnum;
import com.koron.order.risk.base.bean.po.LineRiskChange;
import com.koron.order.risk.base.bean.po.LineRiskChangeMeasure;
import com.koron.order.risk.base.bean.po.LineRiskMeasure;
import com.koron.order.risk.base.bean.vo.LineRiskChangeVO;
import com.koron.order.risk.base.bean.vo.LineRiskVO;
import com.koron.order.risk.base.mapper.LineRiskChangeMapper;
import com.koron.order.risk.base.mapper.LineRiskMeasureMapper;
import com.koron.order.risk.base.service.LineRiskChangeService;
import com.koron.order.risk.base.service.LineRiskService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Service
public class LineRiskChangeServiceImpl implements LineRiskChangeService {

    @Autowired
    private LineRiskService lineRiskService;


    @TaskAnnotation("getRiskChangeById")
    @Override
    public LineRiskChangeVO getRiskChangeById(SessionFactory factory, String id) {
        LineRiskChangeMapper mapper = factory.getMapper(LineRiskChangeMapper.class);
        return mapper.getRiskMeasureById(id);
    }

    @TaskAnnotation("page")
    @Override
    public PageInfo page(SessionFactory factory, PageQuery pageQuery, LineRiskChange lineRiskChange) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        LineRiskChangeMapper mapper = factory.getMapper(LineRiskChangeMapper.class);
        List<LineRiskChange> list = mapper.list(lineRiskChange);
        if (CollectionUtil.isNotEmpty(list)){
            list.forEach(x-> x.setLineRisk(lineRiskService.getById(factory, x.getRiskId())));
        }
        return new PageInfo<>(list);
    }

    @TaskAnnotation("saveOrUpdate")
    @Override
    public LineRiskChange saveOrUpdate(SessionFactory factory, LineRiskChange lineRiskChange) {
        LineRiskChangeMapper mapper = factory.getMapper(LineRiskChangeMapper.class);
        if (StrUtil.isBlank(lineRiskChange.getId())) {
            lineRiskChange.setId(CodeTools.getCode32());
            Assert.notNull(lineRiskChange.getRiskId(), "关联的风险管控单id不能为空！");
            LineRiskVO lineRiskVO = lineRiskService.getById(factory, lineRiskChange.getRiskId());
            lineRiskChange.setCode(CommonCodeTools.getNormalOrderCode(lineRiskVO.getCode(), OrderCodeEnum.FXGK));
        }

        List<LineRiskChangeMeasure> lineRiskChangeMeasures = lineRiskChange.getLineRiskChangeMeasures();
        if (CollectionUtil.isNotEmpty(lineRiskChangeMeasures)) {
            lineRiskChangeMeasures.stream().forEach(c -> {
                if (StrUtil.isBlank(c.getId())) {
                    c.setId(CodeTools.getCode32());
                    c.setChangeId(lineRiskChange.getId());
                }
            });
            mapper.saveOrUpdateChangeMeasure(lineRiskChangeMeasures);
        }

        int result = mapper.saveOrUpdate(lineRiskChange);
        return lineRiskChange;
    }

    @TaskAnnotation("delete")
    @Override
    public Boolean delete(SessionFactory factory, String id) {
        LineRiskChangeMapper mapper = factory.getMapper(LineRiskChangeMapper.class);
        int result = mapper.delete(id);
        return result > 0;
    }

    @TaskAnnotation("updateStatus")
    @Override
    public Boolean updateStatus(SessionFactory factory, String id, Integer status) {
        LineRiskChangeMapper mapper = factory.getMapper(LineRiskChangeMapper.class);
        int result = mapper.updateStatus(id, status);
        return result > 0;
    }

    @TaskAnnotation("updateMeasure")
    @Override
    public Boolean updateMeasure(SessionFactory factory, LineRiskChange lineRiskChange) {
        Assert.notNull(lineRiskChange,"lineRiskChange 不能为空！");
        LineRiskChangeMapper mapper = factory.getMapper(LineRiskChangeMapper.class);
        LineRiskMeasureMapper measureMapper = factory.getMapper(LineRiskMeasureMapper.class);
        int result1 = mapper.updateStatus(lineRiskChange.getId(), lineRiskChange.getStatus());

        List<Map<String, Object>> measureIds = mapper.getAllMeasureById(lineRiskChange.getId());

        AtomicInteger result2 = new AtomicInteger();
        if (CollectionUtil.isNotEmpty(measureIds)){
            measureIds.stream().forEach(m -> {
                LineRiskMeasure lineRiskMeasure = new LineRiskMeasure();
                lineRiskMeasure.setId((String) m.get("id"));
                lineRiskMeasure.setPlanFinishTime((Date) m.get("changeTime"));
                result2.set(measureMapper.update(lineRiskMeasure));
            });
        }
        return result1 > 0 && result2.get() > 0;
    }
}
